
package com.departmodule.servlet;
import com.departmodule.bean.TbDepart;
import com.departmodule.dao.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbDepartServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        if("add".equals(method)){
        String departName=request.getParameter("departName");
        int parentDepartId=Integer.parseInt(request.getParameter("parentDepartId"));
        String departMemo=request.getParameter("departMemo");
            if(new TbDepartDAO().add(new TbDepart(departName,parentDepartId,departMemo,new UtilDAO().decodeDate()))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
             request.getSession(true).setAttribute("tbDepartList",new TbDepartDAO().getTbDepartAll());
            response.sendRedirect("DepartModule/TbDepartList.jsp");
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_Depart", "DepartId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
             request.getSession(true).setAttribute("tbDepartList",new TbDepartDAO().getTbDepartAll());
            response.sendRedirect("DepartModule/TbDepartList.jsp");
        }
        if("edit".equals(method)){
        int departId=Integer.parseInt(request.getParameter("departId"));
        String departName=request.getParameter("departName");
        int parentDepartId=Integer.parseInt(request.getParameter("parentDepartId"));
        String departMemo=request.getParameter("departMemo");

            if(new TbDepartDAO().edit(new TbDepart(departId,departName,parentDepartId,departMemo,new UtilDAO().decodeDate()))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
             request.getSession(true).setAttribute("tbDepartList",new TbDepartDAO().getTbDepartAll());
             request.getSession().removeAttribute("tbDepart");
            response.sendRedirect("DepartModule/TbDepartList.jsp");
        }
        if ("list".equals(method)) {
        	 request.getSession().setAttribute("hashDepart", new TbDepartDAO().getDepart());
             request.getSession(true).setAttribute("tbDepartList",new TbDepartDAO().getTbDepartAll());
            response.sendRedirect("DepartModule/TbDepartList.jsp");
        	}
        if ("listById".equals(method)) {
        	
        //	String departId = request.getParameter("departId");
        	String parDepartId=request.getParameter("parDepartId");
        	//System.out.println("目录"+departId);
        	System.out.println("父目录"+parDepartId);
        	request.getSession(true).setAttribute("tbDepartList",new TbDepartDAO().getTbDepartAll());
             request.getSession(true).setAttribute("tbDepart",new TbDepartDAO().getTbDepartByDepartId(Integer.parseInt(request.getParameter("departId"))));
            // request.getSession().setAttribute("departId", departId);
             request.getSession().setAttribute("parDepartId", parDepartId);
            response.sendRedirect("DepartModule/TbDepartOperate.jsp");
    	}
        if("showadd".equals(method)){
        	 request.getSession(true).setAttribute("tbDepartList",new TbDepartDAO().getTbDepartAll()); 
	         response.sendRedirect("DepartModule/TbDepartOperate.jsp");
	    }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
